<?php
namespace App\Libs\Message;

use App\Models\Message\Message as MsgModel;

class Message
{

    public static function addMsg($type,$data,$user){
        switch ($type){
            //收益提醒
            case 'shouyitixing':
                if(!isset($data['money']) || !isset($data['name']) ){
                    throw \Exception('data中缺少：money:收益数目钱数,name：推荐的用户昵称');
                }
                $title = '收益提醒';
                $txt = "恭喜您！ 成功推荐1位爱豆为家长村会员，获得收益{$data['money']}元，您可在推广——“我的推广”中 查看到账收益。";
                $msg = new MsgModel();
                $msg->uid = $user->id;
                $msg->type = 1;
                $msg->subtype=1;
                $msg->hread = 0;
                $msg->title = $title;
                $msg->content = $txt;
                $msg->data = json_encode(array_merge($data,['time'=>date('Ymd H:i')]));
                $msg->ctime = date('Y:m:d H:i:s');
                $msg->utime = date('Y:m:d H:i:s');
                if($msg->save()){
                    return TRUE;
                }else{
                    return FALSE;
                }
            break;
                //发起体现
            case 'tixianfaqi':
                if(!isset($data['bank']) || !isset($data['money'])){
                    throw \Exception('data中缺少：money:体现金额，bank,银行信息');
                }
                $title = '提现发起';
                $txt = "您好！本次发起提现申请{$data['money']}元，预计7天内到账。";
                $msg = new MsgModel();
                $msg->uid = $user->id;
                $msg->type = 1;
                $msg->subtype=2; //提现发起
                $msg->hread = 0;
                $msg->title = $title;
                $msg->content = $txt;
                $msg->data = json_encode(array_merge($data,['pulltime'=>date('Y:m:d H:i:s')]));
                $msg->ctime = date('Y:m:d H:i:s');
                $msg->utime = date('Y:m:d H:i:s');
                if($msg->save()){
                    return TRUE;
                }else{
                    return FALSE;
                }
            break;
                //到账
            case 'tixiandaozhang':
                if(!isset($data['bank']) || !isset($data['money'])){
                    throw \Exception('data中缺少：bank,银行信息，money:到账金额');
                }
                $title = '提现到账';
                $txt = "您好！您发起提现{$data['money']}元的申请，已到账至支付宝，请查收。";
                $msg = new MsgModel();
                $msg->uid = $user->id;
                $msg->type = 1;
                $msg->subtype=3;//到账信息
                $msg->hread = 0;
                $msg->title = $title;
                $msg->content = $txt;
                $msg->data = json_encode(array_merge($data,['status'=>'已到账，请查收']));
                $msg->ctime = date('Y:m:d H:i:s');
                $msg->utime = date('Y:m:d H:i:s');
                if($msg->save()){
                    return TRUE;
                }else{
                    return FALSE;
                }
                break;
            case 'tiaozhanjiangjin':
                if(!isset($data['className']) || !isset($data['classScore']) || !isset($data['gainScore']) || !isset($data['lastDay']) || !isset($data['classid'])){
                    throw \Exception('data中缺少：className,班级名称，classScore：班级总学分，gainScore:获得学分,lastDay:持续天数');
                }
                $title = '挑战奖金';
                $txt = "恭喜您！在本次挑战{$data['className']}打卡任务中，坚持完成{$data['lastDay']}天的挑战任务共收获{$data['gainScore']}学分，坚持就是改变，做个睿智的爸妈~。";
                $msg = new MsgModel();
                $msg->uid = $user->id;
                $msg->type = 2;
                $msg->hread = 0;
                $msg->title = $title;
                $msg->content = $txt;
                $msg->data = json_encode($data);
                $msg->ctime = date('Y:m:d H:i:s');
                $msg->utime = date('Y:m:d H:i:s');
                if($msg->save()){
                    return TRUE;
                }else{
                    return FALSE;
                }
                break;
            case 'xuanshanghongbao':
                if(!isset($data['className']) || !isset($data['gainScore']) || !isset($data['classid'])){
                    throw \Exception('data中缺少：className,班级名称，gainScore:获得学分');
                }
                $title = '悬赏红包';
                $txt = "恭喜您！在本次悬赏{$data['className']}打卡任务中，坚持完成所有任务共收获{$data['gainScore']}学分，耐心爸妈就是你！下次保持好成绩哦。";
                $msg = new MsgModel();
                $msg->uid = $user->id;
                $msg->type = 2;
                $msg->hread = 0;
                $msg->title = $title;
                $msg->content = $txt;
                $msg->data = json_encode($data);
                $msg->ctime = date('Y:m:d H:i:s');
                $msg->utime = date('Y:m:d H:i:s');
                if($msg->save()){
                    return TRUE;
                }else{
                    return FALSE;
                }
                break;

            case 'system':
                if(!isset($data['content'])){
                    throw \Exception('content:内容');
                }
                $title = '系统消息';
                $txt = $data['content'];
                $msg = new MsgModel();
                $msg->uid = $user->id;
                $msg->type = 3;
                $msg->hread = 0;
                $msg->title = $title;
                $msg->content = $txt;
                $msg->ctime = date('Y:m:d H:i:s');
                $msg->utime = date('Y:m:d H:i:s');
                if($msg->save()){
                    return TRUE;
                }else{
                    return FALSE;
                }
                break;

        }
    }

//    public static function assign($txt,$varArray=[]){
//        $pattern =[
//            '#\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}#',
//        ];
//        $trans = [
/*            "<?php ?>",*/
//        ];
//
//
//        return preg_replace($pattern, $trans,$txt);
//    }


}